package hr.ctg.web.resource;

import hr.ctg.domain.action.SlikaAction;
import hr.ctg.domain.model.Mjesto;
import hr.ctg.service.dao.MjestoDAO;
import hr.ctg.util.StringUtil;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.international.LocaleSelector;
import org.jboss.seam.servlet.ContextualHttpServletRequest;
import org.jboss.seam.web.AbstractResource;

@Scope(ScopeType.APPLICATION)
@Name("carouselResource")
@BypassInterceptors
public class CarouselResource extends AbstractResource {
	private static final int COUNT = 10;

	@Override
	public void getResource(final HttpServletRequest req,
			final HttpServletResponse res) throws ServletException, IOException {
		final String contextPath = req.getContextPath();
		new ContextualHttpServletRequest(req) {
			@Override
			public void process() throws IOException {
				res.setContentType("text/html");
				// res.setCharacterEncoding("UTF-8");
				// final CacheProvider cacheProvider = (CacheProvider) Component
				// .getInstance(CacheProvider.class);
				final LocaleSelector ls = (LocaleSelector) Component
						.getInstance(LocaleSelector.class);
				final String lang = ls.getLanguage();
				final SlikaAction slikaAction = (SlikaAction) Component
						.getInstance("slikaAction");
				// String carousel = null;
				/*
				 * (String) cacheProvider.get("home", "carousel_" + lang);
				 */
				// if (carousel == null) {
				// final StringBuilder sb = new StringBuilder();
				final BufferedWriter out = new BufferedWriter(
						new OutputStreamWriter(res.getOutputStream()));
				out.write("<div id='slide_holder_inner'>");
				out.write("<div id='row_of_slides'>");

				final List<Mjesto> mjCol = getRecommendedMjesta(lang);
				// final WikiService wikiService = (WikiService) Component
				// .getInstance("wikiService");

				if (mjCol.size() > 0) {
					for (int i = 0; i < mjCol.size(); i++) {
						final Mjesto mjesto = mjCol.get(i);
						out.write("<div class='slide' id='slide_" + (i + 1)
								+ "'>");
						out.write("<div class='multi'>");
						out
								.write("<a href='"
										+ res
												.encodeURL(contextPath
														+ "/guides/mjesta/mjesto.seam?idMjesta="
														+ mjesto.getIdMjesta()
														+ "&l="
														+ lang
														+ "&title="
														+ StringUtil
																.encode(mjesto
																		.getNaziv()))
										+ "'>");

						String imagePath = slikaAction.getPocSlikaPath(mjesto,
								"m");
						out.write("<img width='270' height='170' src='"
								+ imagePath + "' alt='"
								+ StringUtil.encode(mjesto.getNaziv())
								+ "'></img>");
						out.write("</a>");
						out.write("<p class='multi-header'>");
						out
								.write("<a href='"
										+ res
												.encodeURL(contextPath
														+ "/guides/mjesta/mjesto.seam?idMjesta="
														+ mjesto.getIdMjesta()
														+ "&l="
														+ lang
														+ "&title="
														+ StringUtil
																.encode(mjesto
																		.getNaziv()))
										+ "'>");
						out.write(StringUtil.encode(mjesto.getNaziv()));
						out.write("</a>");
						out.write("</p>");
						String opis = mjesto.getOpis(lang).getOpis();// wikiService.getPagePreface(mjesto,
						// 1,lang);
						if (opis != null) {
							out.write("<p>");
							out.write(StringUtil.encode(StringUtil.substring(
									opis, 300)));
							out.write("...</p>");
						}
						out.write("</div></div>");
					}
					out.write("</div>");
					out.write("<div id='carousel_controls'>");
					out.write("<ul id='slide_buttons'>");
					for (int i = 1; i < COUNT; i = i + 2) {
						out.write("<li class='slide_" + i + "'><span>" + i
								+ "</span></li>");
					}
					out.write("</ul>");
					out
							.write("<div id='prev_button' class='prev_button'><span></span></div>");
					out
							.write("<div id='next_button' class='next_button'><span></span></div>");
					out
							.write("<div id='slide_captions' class='slide_captions'><span></span></div>");
					out.write("</div>");

					out.write("</div>");
					// carousel = sb.toString();
					// cacheProvider.put("home", "carousel", carousel);
				}
				// }
				out.flush();
				out.close();
			}
		}.run();
	}

	@Override
	public String getResourcePath() {
		return "/carousel";
	}

	private List<Mjesto> getRecommendedMjesta(final String jezik) {
		final List<Mjesto> recMjesta = new ArrayList<Mjesto>();
		final MjestoDAO mjestoDAO = (MjestoDAO) Component
				.getInstance("mjestoDAO");
		final List<Mjesto> l = mjestoDAO.findTopByPosjetMjestaJezik(100, jezik);

		if (l.size() > 10) {
			for (int i = 0; i < 10; i++) {
				int index = (int) (Math.random() * l.size());
				recMjesta.add(l.get(index));
				l.remove(index);
			}
		} else {
			recMjesta.addAll(l);
		}
		return recMjesta;
	}
}
